package com.wutianyi.arithmetic;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.compress.archivers.zip.ZipUtil;
import org.apache.commons.io.FileUtils;
import org.apache.kahadb.util.ByteArrayInputStream;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipModel;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

public class Zip4jMain
{
    public static void main(String[] args) throws ZipException, IOException
    {
        // ZipFile zipFile = new ZipFile("zip4j.zip");

        File file = new File("myzip4j.zip");
        if (file.exists())
        {
            file.delete();
        }

        ZipParameters zipParameters = new ZipParameters();
        // zipParameters.setAesKeyStrength(128);
        // zipParameters.setPassword("abc");
        // zipParameters.setEncryptFiles(true);
        // zipParameters.setAesKeyStrength(128);
        // zipParameters.setEncryptFiles(true);
        zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        zipParameters.setPassword("123456789");
        zipParameters.setSourceExternalStream(true);
        zipParameters.setFileNameInZip("zip.zip");
        // zipFile.addFile(new File("abc.txt"), zipParameters);
        // zipFile.addFile(new File("def.txt"), zipParameters);

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(output);

        zipOutputStream.putNextEntry(new ZipEntry("abc.txt"));
        FileInputStream inputStream = new FileInputStream(new File("abc.txt"));
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(b)) != -1)
        {
            zipOutputStream.write(b, 0, len);
        }
        zipOutputStream.closeEntry();

        zipOutputStream.putNextEntry(new ZipEntry("def.txt"));
        inputStream = new FileInputStream(new File("def.txt"));
        len = 0;
        while ((len = inputStream.read(b)) != -1)
        {
            zipOutputStream.write(b, 0, len);
        }
        zipOutputStream.closeEntry();
        zipOutputStream.flush();
        zipOutputStream.close();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MyZipEngine myZipEngine = new MyZipEngine(new ZipModel());
        myZipEngine.addStreamToZip(outputStream, new ByteArrayInputStream(output.toByteArray()), zipParameters);
        FileOutputStream o = new FileOutputStream(new File("myzip4j.zip"));
        o.write(outputStream.toByteArray());

        o.flush();
        o.close();
        System.out.println("out");
        // zipFile.setComment("请用QQ号进行解密");

        InputStream[] inputStreams = new InputStream[]
        { new FileInputStream(new File("abc.txt")), new FileInputStream(new File("def.txt")) };
        String[] names = new String[]
        { "abc.txt", "def.txt" };

        ByteArrayOutputStream multiOutputStream = new ByteArrayOutputStream();
        MyZipEngine zipEngine = new MyZipEngine(new ZipModel());
        zipEngine.addStreamToZips(multiOutputStream, inputStreams, names, zipParameters);

        FileOutputStream ops = new FileOutputStream(new File("multizip.zip"));
        ops.write(multiOutputStream.toByteArray());
        ops.flush();
        ops.close();
    }

}
